<?php
if(!file_exists($_ENV["HOME"]."/.jdesktop/config.config")){
	 echo("Configure file missing");
	 echo("\nCreating default config....");
	 copy("/usr/share/jdesktop/config.default",$_ENV["HOME"]."/.jdesktop/config.config") or die("Error!");	 
	 echo("Done\n");
}
$config = file($_ENV["HOME"]."/.jdesktop/config.config");
$random = array_splice($config,3);
$image = trim($random[array_rand($random)]);
$ext = substr($image, -4, 4);

echo $ext;
echo ("\n");
echo $image;
echo ("\n");
if($ext == (".jpg" or "jpeg")){ 
	$im = imagecreatefromjpeg($image);
	echo ("Done");
}elseif($etx == ".png"){
	$im = imagecreatefrompng($image);
}elseif($etx == ".gif"){
	$im = imagecreatefromgif($image);
}
$im == true or die("Error\n");

if(trim($config[1]) == "Top Right(Auto)"){
	$ix = imagesx($im);
	echo $ix;
	echo("\n");
	$pos_x = $ix - 358;	
	$pos_y = 100;
}else{
	$res = explode(";",$config[2]);
	$pos_x = $res[0];
	$pos_y = $res[1];
}

$cal = imagecreatefrompng("/usr/share/pixmaps/jdesktop/base.png");
$tdim = imagecreatefrompng("/usr/share/pixmaps/jdesktop/today.png");
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocatealpha($im, 0, 0, 0, 100);
$black = imagecolorallocate($im, 0, 0, 0);
imagecopymerge($im,$cal,$pos_x,$pos_y,0,0,258,200,80);
include_once("fagd.php");
include_once("function.php");
include_once("JalaliDate.php");
$font = '/usr/share/pixmaps/jdesktop//FreeFarsi.ttf';
$jmonth = jdate("m");
$jyear  = jdate("Y");
$jday = jdate("j");
$text = fagd(en2fa(strrev($jday)),'fa');
// Add some shadow to the text
imagettftext($im, 45, 0, $pos_x+195, $pos_y+52, $grey, $font, $text);
// Add the text
imagettftext($im, 44, 0, $pos_x+200, $pos_y+50, $black, $font, $text);
$text = fagd(en2fa(jdate("M")),'fa');
// Add some shadow to the text
imagettftext($im, 20, 0, $pos_x+134, $pos_y+22, $grey, $font, $text);
// Add the text
imagettftext($im, 19, 0, $pos_x+136, $pos_y+20, $black, $font, $text);
$text = fagd(en2fa(strrev(jdate("Y"))),'fa');
// Add some shadow to the text
imagettftext($im, 15, 0, $pos_x+134, $pos_y+44, $grey, $font, $text);
// Add the text
imagettftext($im, 14, 0, $pos_x+136, $pos_y+42, $black, $font, $text);
imagettftext($im, 11, 0, $pos_x+2, $pos_y+42, $black, $font, date("d F Y"));
list($year,$month,$day)=jalali_to_gregorian($jyear, $jmonth, 1);
$date_string = mktime(0,0,0,$month,$day,$year);
$day_start = jdate("w",$date_string);
if($day_start==6){
	$day_start=0;
}else{
	$day_start++;
}
$space = null;
$x = 230;
for( $i = 0 ; $i < $day_start; $i++ )
{
	$x  = $x - 37;
}
$current_position = $i;
if(($jmonth>=1)&&($jmonth<=6)){
	$total_days_in_month=31;
}else{
	if(($jmonth>=7)&&($jmonth<=11)){
		$total_days_in_month=30;
	}else{
		$total_days_in_month=29;
	}
}
$y = 20;
$str = $space;
for( $i = 1; $i <= $total_days_in_month ; $i++)
{
	$today = false;
	if( $i == jdate("j") && $jmonth == jdate("n") && $jyear == jdate("Y") ){
		imagecolortransparent($tdim,imagecolorat($tdim,0,0));
		imagecopymerge($im,$tdim,$x-3+$pos_x,$y+$pos_y+45,0,0,32,32,80);

	}
	$current_position++;
	$str = "$i";
	$str = strrev(" $i");
	$text = fagd(en2fa($str),'fa');
	// Add some shadow to the text
	imagettftext($im, 13, 0, $x-3+$pos_x, $y+28+$pos_y+45, $grey, $font, $text);
	// Add the text
	imagettftext($im, 12, 0, $x+$pos_x, $y+25+$pos_y+45, $black, $font, $text);
	$x = $x - 37;
	if( $current_position == 7 )
	{
		$x = 230;
		$y = $y + 25;
		$current_position = 0;
		$str = "";
	}
}
if(!file_exists($_ENV["HOME"]."/.jdesktop/")){ mkdir($_ENV["HOME"]."/.jdesktop/") or die("1Error"); }
if(!is_writable($_ENV["HOME"]."/.jdesktop/out.png") and file_exists($_ENV["HOME"]."/.jdesktop/out.png")){ die("error2"); }
is_writable($_ENV["HOME"]."/.jdesktop/") or die("Error3");
@unlink($_ENV["HOME"]."/.jdesktop/out.png".date("d") - 1);
imagepng($im,$_ENV["HOME"]."/.jdesktop/out.png");
imagedestroy($im);
system("gconftool-2 -s /desktop/gnome/background/picture_filename \"".$_ENV["HOME"]."/.jdesktop/out.png\" -t string");
?>